import { _decorator, Animation, Component, EventKeyboard, Input, input, KeyCode, Node, tween } from 'cc';
import { Tank } from './Tank';
import { Camp, Direction } from '../Define';
import { PLAYER_CONFIG_ID, PLAYER_INVINCIBLE_TIME } from '../Const';
import { AudioManager } from '../manager/AudioManager';
const { ccclass, property } = _decorator;

@ccclass('Player')
export class Player extends Tank {
    public effect: Node = null

    public invincibleTime = 0.0

    public isDie: boolean = false

    public life: number = 0

    onLoad() {
        super.onLoad()
        this.effect = this.node.getChildByName('Effect')
    }

    onEnable() {
        input.on(Input.EventType.KEY_DOWN, this.onKeyDown, this)
        input.on(Input.EventType.KEY_UP, this.onKeyUp, this)
        this.setInvincible()
    }

    onDisable() {
        input.off(Input.EventType.KEY_DOWN, this.onKeyDown, this)
        input.off(Input.EventType.KEY_UP, this.onKeyUp, this)
        this.stopMove()
    }

    onKeyDown(evt: EventKeyboard) {
        switch(evt.keyCode) {
            case KeyCode.KEY_A:
                this.setDirection(Direction.Left)
                break
            case KeyCode.KEY_D:
                this.setDirection(Direction.Right)
                break
            case KeyCode.KEY_W:
                this.setDirection(Direction.Up)
                break;
            case KeyCode.KEY_S:
                this.setDirection(Direction.Down)
                break;
            case KeyCode.SPACE:
                this.shoot()
                break
        }
    }

    onKeyUp(evt: EventKeyboard) {
        switch(evt.keyCode) {
            case KeyCode.KEY_A:
            case KeyCode.KEY_D:
            case KeyCode.KEY_W:
            case KeyCode.KEY_S:
                this.stopMove()
                break
        }
    }

    getCamp() {
        return Camp.Player
    }

    setInvincible() {
        this.invincibleTime = PLAYER_INVINCIBLE_TIME
        this.effect.active = true
        this.effect.getComponent(Animation).play()
        tween().target(this.effect).delay(this.invincibleTime).call(() => {
            this.effect.getComponent(Animation).stop()
            this.effect.active = false
            this.invincibleTime = 0.0
        }).start()
    }

    setDirection(dir: Direction): void {
        super.setDirection(dir)
        AudioManager.inst.play('player_move')
        AudioManager.inst.loop = true
    }

    stopMove(): void {
        super.stopMove()
        AudioManager.inst.stop()
        AudioManager.inst.loop = false
    }

    shoot(): void {
        super.shoot()
        AudioManager.inst.playOneShot("shoot")
    }

    die() {
        this.life--
        this.isDie = true
        this.node.removeFromParent()
        AudioManager.inst.stop()
        AudioManager.inst.playOneShot("player_bomb")
    }

    hasLife() {
        return this.life > 0
    }

    respawn() {
        this.isDie = false
        this.setInvincible()
    }

    isInvincible() {
        return this.invincibleTime > 0.0
    }
}


